package cn.wikiflyer.beikang;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import cn.wikiflyer.beikang.act.UserInfoBean;
import cn.wikiflyer.beikang.act.study.bean.CategoryBean;
import cn.wikiflyer.beikang.act.user.bean.ShareBean;
import cn.wikiflyer.beikang.act.user.bean.UserBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.net.HttpRequest;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.utils.L;
import cn.wk.libs4a.utils.WKCountDownUtils;
import cn.wk.libs4a.utils.WKUmShareUtils;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKListViewUtils.CheckResultOK;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class IApplication extends WKApplication {

	/***
	 * 打包前检查：
	 * 
	 * 1 版本号升级
	 * 
	 * 2 是否是真正付款（isPayTestMoney）
	 * 
	 * 3 服务器地址
	 */

	public static boolean isAutoLogin = false;// 是否自动填上账户
	public static boolean isOpenCookie4Http = true;// 是否打开cookie
	public static boolean levelUp = false; // 是否升级了
	public static boolean isOnlyOneProduct = true; // 是否一个产品
	public static boolean showMyMember = false;// 是否显示会员
	public static boolean shareHomePageToWEIXIN = true;// 是否分享主页链接到微信
	public static boolean isPayTestMoney = true;// 是否用测试的0.01元价钱付款
	public static String loginTxt = "请点右上角设置-登录";// 请登录文本

	public static boolean onlyAlipay = false;// 是否是显示支付宝方式微信
	public static boolean isBackWhen0 = false;// 0时是否返回

	public HttpRequest net = null;

	/**
	 * Umeng
	 */
	public WKUmShareUtils shareUtils;
	public static String weixinAppID = "wxff07008c39db9493";
	public static String weixinAppSecret = "85bdf2461e3a489400204a44ed03146d";
	public static String tencentAppID = "1104546810";
	public static String tencentAppKEY = "WdSroMiygLGmvMNG";

	// public static UMImage shareImage;

	public ArrayList<CategoryBean> studyCates = new ArrayList<CategoryBean>();

	// 推荐好友的文本和链接
	public ShareBean shareTuijian;
	// 资讯分享的文本和链接
	public ShareBean shareFenXiang;

	public static String shareTitle = "前海倍康";

	// 比登录更多的用户信息
	public UserInfoBean userInfo = null;

	// 是否隐藏支付失败对话框
	public boolean isShowZhifuDialog = false;

	// 注册冷却
	public WKCountDownUtils cdUtils = null;

	// 设置界面
	public BaseActivity settingAct;

	// 获得注册邀请码
	public String getShareCode() {
		if (userInfo == null || userInfo.self_code == null
				|| userInfo.self_code.equals("")) {
			return "";
		}
		if (userInfo.level.equals("1")) {
			return "注册邀请码：" + userInfo.parent_code;
		} else {
			return "注册邀请码：" + userInfo.self_code;
		}
	}

	/**
	 * 显示shareview
	 */
	public void shareFenxiang(Activity act) {
		this.shareUtils.showShareView(shareTitle, shareFenXiang.share_text
				+ getShareCode() + " " + shareTuijian.share_url,
				shareFenXiang.share_url, null, act);
	}

	/**
	 * 显示shareview
	 */
	public void shareTuijian(Activity act) {
		this.shareUtils.showShareView(shareTitle, shareTuijian.share_text
				+ getShareCode() + " " + shareTuijian.share_url,
				shareTuijian.share_url, null, act);
	}

	public static IApplication self;

	@Override
	public void onCreate() {
		isSetCrashHandler = true;
		self = this;

		super.onCreate();

		net = new HttpRequest(getApplicationContext());
		net.setLoginCookie(true);

		// BASE_URL = "http://test.conglincode.com/";
		// BASE_URL = "http://bk.ngrok.io/index.php";
		// BASE_URL = "http://115.28.41.231/index.php";
		// BASE_URL = "http://test.conglincode.com/";
		// BASE_URL = "http://115.28.41.231/index.php";
//		 BASE_URL = "http://bk.ngrok.io/index.php";
		BASE_URL = "http://www.qhbeikang.com/index.php";

		// 分享相关
		shareUtils = new WKUmShareUtils(this, weixinAppID, weixinAppSecret,
				tencentAppID, tencentAppKEY);
		// 设置微信好友分享内容
		// 设置分享图片
		Drawable dra = getResources().getDrawable(R.drawable.tab3);
		BitmapDrawable bd = (BitmapDrawable) dra;
		Bitmap bm = bd.getBitmap();

		// 设置具体项目的配置
		WKHeader.HEADER_BACK_ICO_ID = R.drawable.ico_back;
		WKHeader.HEADER_BG_COLOR = R.color.color_style;
		WKHeader.HEADER_TITLE_COLOR = R.color.white;
		WKHeader.HEADER_RIGHT_TXT_COLOR = R.color.white;

		// 设置result检测方法
		WKListViewUtils.setCheck(new CheckResultOK() {
			public boolean checkResultOK(String result) {
				if (result.contains("\"status\":1"))
					return true;
				return false;
			}
		});
		// 测试
		// WKWebAct.testForBK = true;
		// WKWebAct.share = new Share() {
		// public void share(String title, String url) {
		// shareUtils.initUmShare();
		// }
		// };

		// 分享相关
		shareFenXiang = new ShareBean();
		shareFenXiang.share_text = "加入倍康，财富和健康双收，2015会员限时开放注册，加入我们开启您新的事业，免费下载APP";
		shareFenXiang.share_url = "http://www.qhbeikang.com";
		shareTuijian = new ShareBean();
		shareTuijian.share_text = "邀请您加入倍康，财富和健康双收，2015会员限时开放注册，加入我们开启您新的事业，点击链接注册成为倍康会员，尊享更多优惠和特权。";
		shareTuijian.share_url = "http://www.qhbeikang.com";

		cdUtils = new WKCountDownUtils(null, "获取验证码", 60 * 1, 1);

		this.isOnlySavePostCookie = true;
	}

	/*** 用户信息 *********************/
	/**
	 * 存储用户登录数据
	 */
	public void setLoginSession(String data) {
		sp.setString("usersession", data);
		UserBean ub = getLoginSession();
		if (ub == null) {
			setCacheKey("");
		} else {
			setCacheKey(ub.userid);
		}
	}

	/**
	 * 获取用户数据
	 */
	public UserBean getLoginSession() {
		String data = sp.getString("usersession");
		L.wj("data : " + data);
		if (!TextUtils.isEmpty(data)) {
			Type cvbType = new TypeToken<BKBaseBean<UserBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<UserBean> bean = gson.fromJson(data, cvbType);
			return bean.data;
		}
		return null;
	}
}
